class Order < ActiveRecord::Base
  belongs_to :customer
  
  has_many :items
  
  has_many :shippings
  has_many :payments
  # has_and_belongs_to_many :invoices,  :join_table => :orders_invoices
  has_many :orders_invoices
  has_many :invoices, :through => :orders_invoices
  has_one :production_schedule
  
  
  accepts_nested_attributes_for :items
  
  #====== validations =========
  validates_presence_of :order_code, :customer_id, :order_type
  validates_numericality_of :amount
  validates :order_type, :inclusion => { :in => %w{TEMP REG} }
  
  
  def set_order_code 
    self.order_code = "SO-"+Time.now.strftime("%Y-%m-%d-%H%M%S")
  end
  
  def set_order_date
    self.order_date = Time.now.strftime("%Y-%m-%d")
  end
  
  # if an order is audited, only admin can delete it.
  # def valid?
#     
  # end 
  
end
